package com.snail.mapper;

import com.snail.model.pojo.cart.BizRefundRequest;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface BizRefundRequestMapper {



    // 退款申请
    @Insert("INSERT INTO biz_refund_requests (order_no, product_id, seller_user_id, quantity, refund_reason, refund_status, create_time, update_time) " +
            "VALUES (#{orderNo}, #{productId}, #{sellerUserId}, #{quantity}, #{refundReason}, #{refundStatus}, #{createTime}, #{updateTime})")
    void createRefundRequest(BizRefundRequest refundRequest);

    //查询退款申请
    @Select("SELECT * FROM biz_refund_requests WHERE user_id = #{userId} AND status = 1")
    List<BizRefundRequest> getRefundRequestByUserId(Long userId);

    //根据用户id修改退款申请，修改退款金额和退款原因
    @Update("UPDATE biz_refund_requests SET refund_amount = #{refundAmount}, reason = #{reason} WHERE user_id = #{userId}")
    void updateRefundRequest(BizRefundRequest refundRequest);

    //根据用户id取消退款申请
    @Update("UPDATE  biz_refund_requests SET status=#{status} WHERE user_id = #{userId}")
    void quitRefundRequest(@Param("userId") Long userId, @Param("status") Integer status);


    List<BizRefundRequest> getSimpleRefundByUserId(Long userId);


    /**
     * 插入退款记录
     */
    @Insert("INSERT INTO biz_refund_requests (" +
            "order_id, user_id, refund_no, payment_id, refund_amount, " +
            "reason, status, create_time, update_time" +
            ") VALUES (" +
            "#{orderId}, #{userId}, #{refundNo}, #{paymentId}, #{refundAmount}, " +
            "#{reason}, #{status}, #{createTime}, #{updateTime}" +
            ")")
    @Options(useGeneratedKeys = true, keyProperty = "refundId")
    int insert(BizRefundRequest refundRequest);

    /**
     * 查询订单是否有未完成的退款申请
     */
    @Select("SELECT COUNT(*) FROM biz_refund_requests " +
            "WHERE order_id = #{orderId} AND status IN (1, 2)")
    int countUnfinishedByOrderNo(@Param("orderNo") String orderNo);
}
